package com.chuji.crmcj.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.chuji.crmcj.R;
import com.chuji.crmcj.bean.FullPayInfo;
import com.chuji.crmcj.bean.GoodMan;
import com.chuji.crmcj.utils.NumberUtils;
import com.chuji.crmcj.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by w.pitt on 2016/3/8.
 */
public class TenureAdapter extends BaseAdapter {

    private Context mContext;
    private ListView mLvFullPay;
    private List<FullPayInfo.ApiParamObjEntity> fullPayInfo;
    private ArrayList<GoodMan> mPersons;
    private String mFirstPinyin;
    private String mPreFirstPinyin;

    public List<FullPayInfo.ApiParamObjEntity> getList() {
        return fullPayInfo;
    }

    public TenureAdapter(Context mContext, ListView mLvFullPay, List<FullPayInfo.ApiParamObjEntity> fullPayInfo,
                         ArrayList<GoodMan> mPersons) {
        this.mContext = mContext;
        this.mLvFullPay = mLvFullPay;
        this.fullPayInfo = fullPayInfo;
        this.mPersons = mPersons;
    }

    @Override
    public int getCount() {
        return fullPayInfo.isEmpty() ? 0 : fullPayInfo.size();
    }

    @Override
    public Object getItem(int i) {
        return fullPayInfo.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    private ViewHolder holder;

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = View.inflate(UIUtils.getContext(), R.layout.item, null);
            holder = new ViewHolder();
            ImageView mIvLevel = (ImageView) view.findViewById(R.id.iv_level);
            mIvLevel.setVisibility(View.GONE);
            holder.tvPinyin = (TextView) view.findViewById(R.id.tv_pinyin);
            holder.mTvDealCar = (TextView) view.findViewById(R.id.tv_want_car);
//          holder.mTvSalerName = (TextView) view.findViewById(R.id.tv_sale_name);
            holder.mTvLeastTime = (TextView) view.findViewById(R.id.tv_least_time);
            holder.mTvCusName = (TextView) view.findViewById(R.id.tv_name);
            holder.mTvCusNum = (TextView) view.findViewById(R.id.tv_number);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.mTvDealCar.setText(fullPayInfo.get(position).getCarModel());
        String tel = fullPayInfo.get(position).getTel();
        if (tel.length() == 11) {
            holder.mTvCusNum.setText(NumberUtils.parsePhoneNumber(tel));
        } else {
            holder.mTvCusNum.setText(tel);
        }
        holder.mTvLeastTime.setText(fullPayInfo.get(position).getCreateTime());
        holder.mTvCusName.setText(fullPayInfo.get(position).getName());

        GoodMan goodMan = mPersons.get(position);
        mFirstPinyin = String.valueOf(goodMan.mPinyin.charAt(0));
        if (position == 0) {
            mPreFirstPinyin = "-";
        } else {
            mPreFirstPinyin = String.valueOf(mPersons.get(position - 1).mPinyin.charAt(0));
        }
        holder.tvPinyin.setVisibility(TextUtils.equals(mPreFirstPinyin, mFirstPinyin) ? View.GONE
                : View.VISIBLE);
        holder.tvPinyin.setText(String.valueOf(goodMan.mPinyin.charAt(0)));
        return view;
    }

    static class ViewHolder {
        private TextView mTvDealCar;
        private TextView mTvSalerName;
        private TextView mTvCusName;
        private TextView mTvCusNum;
        private TextView mTvLeastTime;
        private TextView tvPinyin;
    }

}
